﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using DevExpress.XtraBars.ToolbarForm;
using System;
using System.Windows.Forms;
using DevExpress.XtraCharts.Designer;
using XCI.Helper;
using XCI.Windows;

namespace XCI.Setting
{
    public partial class FrmSettingChartForm : ToolbarForm
    {
        private ChartControlPlus _chartControl;
        public FrmSettingChartForm()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (WinFormHelper.IsDesignMode()) WinFormHelper.InitDesignFont();
        }

        public void Init(ChartControlPlus chartControl)
        {
            this._chartControl = chartControl;
            this.barId.EditValue = chartControl.Id;
            TabDelayLoad.Init(tabPane1, name =>
            {
                switch (name)
                {
                    case "tabTemplate":
                        gridTemplate1.Init(_chartControl.SettingManager, "chart_:_");
                        break;
                    case "tabAdvanced":
                        gridAdvanced1.SelectedObject = _chartControl;
                        break;
                }
            });
        }

        private void Form_Load(object sender, EventArgs e)
        {
            WinFormHelper.CenterControl(this, btnChartDesigner, tabChartDesigner, true, true);
        }

        private async void btnReset_Click(object sender, EventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要重置图表配置吗？重置后此图表的配置会全部删除") != DialogResult.Yes) return;

            await _chartControl.SettingManager.DeleteAllAsync();
            this.Close();
        }

        private async void btnSave_Click(object sender, EventArgs e)
        {
            await _chartControl.SettingManager.SaveCurrentAsync();
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private async void btnRestoreDefault_Click(object sender, EventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要恢复图表默认配置吗？恢复会丢失当前配置") != DialogResult.Yes) return;

            await _chartControl.SettingManager.LoadDefaultAsync();
            MessageBoxHelper.ShowMessage("恢复图表默认配置成功,请点保存确认");
            this.Close();
        }

        private async void btnSaveDefault_Click(object sender, EventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要把当前配置设为默认配置吗？") != DialogResult.Yes) return;

            await _chartControl.SettingManager.SaveDefaultAsync();
            MessageBoxHelper.ShowMessage("默认配置设置成功");
        }

        private void BtnChartDesigner_Click(object sender, EventArgs e)
        {
            ChartDesigner designer = new ChartDesigner(_chartControl);
            designer.ShowDialog();
        }

        private void FrmSettingChartForm_SizeChanged(object sender, EventArgs e)
        {
            WinFormHelper.CenterControl(this, btnChartDesigner, tabChartDesigner, true, true);
        }
    }
}